var betterga_bundle = Components.classes["@mozilla.org/intl/stringbundle;1"].getService(Components.interfaces.nsIStringBundleService);
var betterga_usLabels = betterga_bundle.createBundle("chrome://betterga/locale/userscript.properties");

var betterga_tabs = { GENERAL:0, REPORTS:1, OTHER:2 };

function betterga_userScript( id, enabled, full_name, author, version, homepage, filename, description, enabled_by_default, tab, conflicts ){
	this.id = id;
	this.enabled = enabled;
	this.full_name = full_name;
	this.author = author;
	this.version = version;
	this.homepage = homepage;
	this.filename = filename;
	this.description = description;
	this.enabled_by_default = enabled_by_default;
	this.tab = tab;
	this.conflicts = conflicts;
}

function betterga_makeNewUserScript( id, author, version, homepage, enabled_by_default, tab ){
	//alert("making " + id);
	return new betterga_userScript(	id,
									betterga_getOrElseSet(id, enabled_by_default),
									betterga_usLabels.GetStringFromName(id+"_title"),
									author,
									version,
									homepage,
									id+".user.js",
									betterga_usLabels.GetStringFromName(id+"_desc"),
									enabled_by_default,
									tab,
									null,
									null);
}

function betterga_makeNewUserScriptWithConflicts( id, author, version, homepage, enabled_by_default, tab, conflicts ){
	return new betterga_userScript(	id,
									betterga_getOrElseSet(id, enabled_by_default),
									betterga_usLabels.GetStringFromName(id+"_title"),
									author,
									version,
									homepage,
									id+".user.js",
									betterga_usLabels.GetStringFromName(id+"_desc"),
									enabled_by_default,
									tab,
									conflicts,
									null);
}

function betterga_makeNewSkin( id, author, version, homepage ){
	return new betterga_userScript(	id,
									betterga_getOrElseSet( id, false ),
									betterga_usLabels.GetStringFromName( id+"_title" ),
									author,
									version,
									homepage, 
									id+".user.js",
									betterga_usLabels.GetStringFromName( id+"_desc" ),
									false,
									betterga_tabs.SKINS,
									null,
									null);
}

function betterga_getOrElseSet( script, defaultValue ){
	var k = "betterga.enabled." + script;

	if( !betterga_isPreferenceSet( k ) ){
		betterga_setBooleanPreference( k, defaultValue );
	}

	return betterga_getBooleanPreference( k );
}

var betterga_scripts = new Array(
	new betterga_makeNewUserScript(	'autoAccessAnalytics',
									'Erik Vold',
									'1.2',
									'http://userscripts.org/scripts/show/35588',
									true,
									betterga_tabs.GENERAL),

	new betterga_makeNewUserScript(	'gaAutoExpandNav',
									'Erik Vold',
									'0.1',
									'http://userscripts.org/scripts/show/56182',
									false,
									betterga_tabs.GENERAL),

	new betterga_makeNewUserScript(	'gaDeAnalyse',
									'Brian Katz',
									'1',
									'http://userscripts.org/scripts/show/47121',
									false,
									betterga_tabs.GENERAL),

	new betterga_makeNewUserScript(	'addSearchToGAKeywordReports',
									'Michael Gall',
									'0.3',
									'http://userscripts.org/scripts/show/57202',
									true,
									betterga_tabs.REPORTS),

	new betterga_makeNewUserScript(	'changeMenuItemDefaultsForGA',
									'Erik Vold',
									'1.0.1',
									'http://userscripts.org/scripts/show/55643',
									true,
									betterga_tabs.REPORTS),

	new betterga_makeNewUserScript(	'cleanerGAProfileSwitching',
									'Erik Vold',
									'1.1.1',
									'http://userscripts.org/scripts/show/29874',
									true,
									betterga_tabs.REPORTS),

	new betterga_makeNewUserScript(	'exportToGoogleDocsFromGA',
									'Michael Freeman',
									'0.4',
									'http://userscripts.org/scripts/show/27673',
									true,
									betterga_tabs.REPORTS),

	new betterga_makeNewUserScript(	'gaAbsoluteConversion',
									'Arty V',
									'2.1',
									'http://userscripts.org/scripts/show/32972',
									true,
									betterga_tabs.REPORTS),

	new betterga_makeNewUserScript(	'gaAdvancedDateSelection',
									'Tyson Kirksey',
									'0.4',
									'http://userscripts.org/scripts/show/31584',
									true,
									betterga_tabs.REPORTS),

	new betterga_makeNewUserScript(	'gaFullScreenReports',
									'Tyson Kirksey',
									'0.2',
									'http://userscripts.org/scripts/show/45686',
									true,
									betterga_tabs.REPORTS),

	new betterga_makeNewUserScript(	'gaProfileSettingsLink',
									'Erik Vold',
									'0.1',
									'http://userscripts.org/scripts/show/55648',
									true,
									betterga_tabs.REPORTS),

	new betterga_makeNewUserScript(	'gaQuickContentSearch',
									'Tyson Kirksey',
									'0.1',
									'http://userscripts.org/scripts/show/49618',
									true,
									betterga_tabs.REPORTS),
/*
	new betterga_makeNewUserScript(	'gaRowLimit',
									'Erik Vold'
									'',
									'http://userscripts.org/scripts/show/54015',
									true,
									betterga_tabs.REPORTS),
*/
	new betterga_makeNewUserScript(	'gaRowLimitLink',
									'Erik Vold',
									'0.1',
									'http://userscripts.org/scripts/show/56369',
									true,
									betterga_tabs.REPORTS),

	new betterga_makeNewUserScript(	'gaSorter',
									'Andre Scholten',
									'1.2',
									'http://userscripts.org/scripts/show/31618',
									true,
									betterga_tabs.REPORTS),

	new betterga_makeNewUserScript(	'insightsForGA',
									'Mike Belasco',
									'1.0.1',
									'http://userscripts.org/scripts/show/57267',
									true,
									betterga_tabs.REPORTS),

	new betterga_makeNewUserScript(	'smmPluginForGA',
									'Erik Vold',
									'1.2.1',
									'http://userscripts.org/scripts/show/35080',
									true,
									betterga_tabs.REPORTS),

	new betterga_makeNewUserScript(	'gaConversionUniversityExpandAllButton',
									'Erik Vold',
									'0.1',
									'http://userscripts.org/scripts/show/56258',
									true,
									betterga_tabs.OTHER),

	new betterga_makeNewUserScript(	'gaConversionUniversityShowTime',
									'Erik Vold',
									'0.1',
									'http://userscripts.org/scripts/show/56403',
									true,
									betterga_tabs.OTHER),

	new betterga_makeNewUserScript(	'gaURLBuilderOverrideOption',
									'Erik Vold',
									'0.1',
									'http://userscripts.org/scripts/show/56266',
									true,
									betterga_tabs.OTHER)
);

var betterga_enabledScripts = null;

function betterga_getEnabledScripts( betterga_scripts){
	betterga_enabledScripts = new Array();

	for( var i = 0; i < betterga_scripts.length; i++ ){
		if( betterga_getBooleanPreference( "betterga.enabled."+ betterga_scripts[i].id ) ){
			betterga_enabledScripts.push( betterga_scripts[i] );
			//DEBUG INFO
			//alert( betterga_scripts[i].id + "enabled");
		}
	}

	betterga_setBooleanPreference( "betterga.loaded", true );
}

betterga_getEnabledScripts( betterga_scripts );

function betterga_isScriptApplicable( script, href ){
	var result = false;

	switch(script){
		case 'addSearchToGAKeywordReports':
			result = ( /https?:\/\/((adwords|www)\.)?google.com\/analytics\/reporting\/(keywords|search_engine_detail)\?.*/.test(href) ) && true;
			break;
		case 'autoAccessAnalytics':
			result = ( /https?:\/\/(www\.)?google\.c(om|a)(\/intl\/.{2,10})?\/analytics\/()?/.test(href) ) && true;
			break;
		case 'cleanerGAProfileSwitching':
			result = ( /https?:\/\/((adwords|www)\.)?google\.com\/analytics\/(reporting\/|settings\/profile_summary).*/.test(href) ) && true;
			break;
		case 'changeMenuItemDefaultsForGA':
			result = ( /https?:\/\/((www)\.)?google\.com\/analytics\/reporting\/.*/.test(href) ) && true;
			break;
		case 'exportToGoogleDocsFromGA':
			result = ( /https:\/\/www\.google\.com\/analytics\/reporting\/.*/.test(href) || /https:\/\/www\.google\.com\/accounts\/ServiceLoginBox\?service=analytics.*/.test(href) ) && true;
			break;
		case 'gaAbsoluteConversion':
			result = ( /https?:\/\/(www\.)?google.com\/analytics\/reporting\/.*/.test(href) && !( /https?:\/\/(www\.)?google.com\/analytics\/reporting\/(top_content|content_titles|content_drilldown).*/.test(href)) ) && true;
			break;
		case 'gaAdvancedDateSelection':
			result = ( /https?:\/\/((adwords|www)\.)?google.com\/analytics\/.*/.test(href) ) && true;
			break;
		case 'gaAutoExpandNav':
			result = ( /https?:\/\/(www\.)?google.com\/analytics\/reporting.*/.test(href) ) && true;
			break;
		case 'gaConversionUniversityExpandAllButton':
			result = ( /https?:\/\/www\.google.com\/support\/conversionuniversity\/.*/.test(href) ) && true;
			break;
		case 'gaConversionUniversityShowTime':
			result = ( /https?:\/\/www\.google.com\/support\/conversionuniversity\/.*/.test(href) ) && true;
			break;
		case 'gaDeAnalyse':
			result = ( /https?:\/\/(www\.)?google.com\/analytics\/(settings|reporting)\/.*/.test(href) ) && true;
			break;
		case 'gaFullScreenReports':
			result = ( /https?:\/\/(www\.)?google.com\/analytics\/.*/.test(href) ) && true;
			break;
		case 'gaProfileSettingsLink':
			result = ( /https?:\/\/((www)\.)?google\.com\/analytics\/(reporting|settings)\/.*/.test(href) ) && true;
			break;
		case 'gaQuickContentSearch':
			result = ( /https?:\/\/(www\.)?google\.com\/analytics\/.*/.test(href) ) && true;
			break;
//		case 'gaRowLimit':
//			result = ( /https?:\/\/((adwords|www)\.)?google\.com\/analytics\/reporting\/.*/.test(href) ) && true;
//			break;
		case 'gaRowLimitLink':
			result = ( /https?:\/\/((adwords|www)\.)?google\.com\/analytics\/reporting\/.*/.test(href) ) && true;
			break;
		case 'gaSorter':
			result = ( /https?:\/\/(www\.)?google\.com\/analytics\/.*/.test(href) ) && true;
			break;
		case 'gaURLBuilderOverrideOption':
			result = ( /https?:\/\/((www)\.)?google\.com\/(support\/googleanalytics|support\/analytics)\/bin\/answer.py\?&?answer=55578.*/.test(href) ) && true;
			break;
		case 'insightsForGA':
			result = ( /https?:\/\/((adwords|www)\.)?google.com\/analytics\/reporting\/(all_source_medium_base_detail|keywords|keyword_detail|search_engines|search_engine_detail)\?.*/.test(href) ) && true;
			break;
		case 'smmPluginForGA':
			result = ( /https?:\/\/((adwords|www)\.)?google\.com\/analytics\/reporting\/content_detail\?.*/.test(href) ) && true;
			break;
	}

	return result;
}